package com.gutsyzhan.dynamicdatasourcespringbootstarter.config;

import lombok.Data;

import java.lang.reflect.Field;
import java.util.Optional;
import java.util.Properties;

/**
 * Druid连接池相关属性
 */
@Data
public class DruidConfig {
    private long connectCount;
    private long closeCount;
    private volatile long connectErrorCount;
    private long recycleCount;
    private long removeAbandonedCount;
    private long notEmptyWaitCount;
    private long notEmptySignalCount;
    private long notEmptyWaitNanos;
    private int keepAliveCheckCount;
    private int activePeak;
    private long activePeakTime;
    private int poolingPeak;
    private long poolingPeakTime;

    public static final int DEFAULT_INITIAL_SIZE = 0;
    public static final int DEFAULT_MAX_ACTIVE_SIZE = 8;
    public static final int DEFAULT_MAX_IDLE = 8;
    public static final int DEFAULT_MIN_IDLE = 0;
    public static final int DEFAULT_MAX_WAIT = -1;

    protected volatile int initialSize = DEFAULT_INITIAL_SIZE;
    protected volatile int maxActive = DEFAULT_MAX_ACTIVE_SIZE;
    protected volatile int minIdle = DEFAULT_MIN_IDLE;
    protected volatile int maxIdle = DEFAULT_MAX_IDLE;
    protected volatile long maxWait = DEFAULT_MAX_WAIT;
    protected int notFullTimeoutRetryCount;

    /**
     * 将字段转成Properties
     */
    public Properties toProperties() {
        Properties properties = new Properties();
        Field[] fields = this.getClass().getDeclaredFields();
        try {
            for (Field field : fields) {
                Optional.ofNullable(field.get(this))
                        .ifPresent(v -> properties.setProperty(field.getName(), v.toString()));
            }
        }catch (Exception e){
            throw new RuntimeException("Druid config toProperties error,e=", e);
        }
        return properties;
    }
}
